package org.eenie.hookui.hook


import android.annotation.SuppressLint
import android.app.Activity
import android.app.Application
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.LinearLayout
import de.robv.android.xposed.*
import de.robv.android.xposed.callbacks.XC_LoadPackage
import org.eenie.hookui.App
import org.eenie.hookui.ui.PluginMainActivity
import org.eenie.hookui.utils.ParasiticManager
import java.lang.reflect.Field


class HookEntry : IXposedHookLoadPackage, IXposedHookZygoteInit {

    private val TARGET_PKG = "org.eenie.loadaar"

    private val PACKAGE_NAME_QQ = "com.tencent.mobileqq"

    private var plugin_path = ""

    private lateinit var clzLoader: ClassLoader


    override fun handleLoadPackage(lpparam: XC_LoadPackage.LoadPackageParam) {

        if (lpparam.packageName.equals(TARGET_PKG)) {
            XposedBridge.log("hook start " + lpparam.packageName)

            clzLoader = lpparam.classLoader




            XposedHelpers.findAndHookMethod(clzLoader.loadClass("org.eenie.loadaar.App"), "onCreate", object : XC_MethodHook() {
                override fun afterHookedMethod(param: MethodHookParam) {
                    XposedBridge.log("app start")
                    val app = param.thisObject as Application


                    ParasiticManager.initForStubActivity(app, plugin_path)
                }
            })

            XposedHelpers.findAndHookMethod(clzLoader.loadClass("org.eenie.loadaar.MainActivity"), "onCreate", Bundle::class.java, object : XC_MethodHook(52) {
                override fun afterHookedMethod(param: MethodHookParam) {
                    val activity = (param.thisObject as Activity)
                    val layRoot = activity.findViewById<LinearLayout>(activity.resources.getIdentifier("lay_root", "id", activity.packageName))
                    val btn = Button(activity)
                    btn.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
                    btn.text = "Load"
                    btn.setOnClickListener {
                        activity.startActivity(Intent(activity, PluginMainActivity::class.java))
                    }
                    layRoot.addView(btn)
                }
            })


        }


    }

    override fun initZygote(startupParam: IXposedHookZygoteInit.StartupParam?) {
        plugin_path = startupParam?.modulePath ?: ""
        XposedBridge.log(plugin_path)
    }


    @SuppressLint("DiscouragedPrivateApi")
    private fun injectClassLoader(classLoader: ClassLoader) {
        try {
            val fParent: Field = ClassLoader::class.java.getDeclaredField("parent")
            fParent.isAccessible = true
            val mine = App::class.java.classLoader
            fParent.set(classLoader, mine)
            XposedBridge.log("注入 end")

        } catch (e: Exception) {
            XposedBridge.log(e)
            e.printStackTrace()
        }
    }


}